using Assets.Scripts.GDK;
using SDK.Scripts.App;
using SDK.Scripts.Module;
using SDK.Scripts.Plat;
using SDK.Scripts.Utils;
using SimpleJson;
using System.Collections.Generic;

namespace FrameWork.Scripts.Module
{
	public class GFW_Advert : BaseModule
	{
		public delegate void CreateAdvertFunc(bool ret, string adId, string adType, string extra, JsonObject obj);

		public delegate void ShowAdvertFunc(int ret, string adId, JsonObject obj);

		public Dictionary<string, CreateAdvertFunc> _adVertList = new Dictionary<string, CreateAdvertFunc>();

		public Dictionary<string, ShowAdvertFunc> _adVertResultList = new Dictionary<string, ShowAdvertFunc>();

		public JsonObject CacheAdvert
		{
			get
			{
				string json = GameSDK.GamePlat.DoAction(platActionID.action_GetAdvertType, string.Empty);
				return (JsonObject)SimpleJson.SimpleJson.DeserializeObject(json);
			}
		}

		public GFW_Advert()
		{
			GameSDK.GamePlat.gamePlatEvent += OnEventBack;
		}

		private void OnEventBack(string methonName, int code, string data)
		{
			if (!(methonName == "action"))
			{
				return;
			}
			JsonObject jsonObject = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(data);
			switch (code)
			{
			case 145:
				gdk_game_manager.log("回调+：event_CreateAdvert");
				if (jsonObject != null && jsonObject.ContainsKey("placementId"))
				{
					string text2 = jsonObject["placementId"].ToString();
					CreateAdvertFunc createAdvertFunc = _adVertList[text2];
					if (createAdvertFunc != null)
					{
						createAdvertFunc(int.Parse(jsonObject["ret"].ToString()) == 1, text2, jsonObject["adType"].ToString(), jsonObject["callbackExtra"].ToString(), jsonObject);
					}
					else
					{
						Debuger.LogWarning("拉取广告成功,但是不存在回调");
					}
					_adVertList.Remove(text2);
					gdk_game_manager.log("存在 移除广告监听");
				}
				else
				{
					Debuger.Log("拉取广告失败,广告ID(场景)不存在");
					gdk_game_manager.log("拉取广告失败,广告ID(场景)不存在");
				}
				break;
			case 146:
				gdk_game_manager.log("回调+event_ShowAdvert");
				if (jsonObject != null && jsonObject.ContainsKey("placementId"))
				{
					string text = jsonObject["placementId"].ToString();
					ShowAdvertFunc showAdvertFunc = _adVertResultList[text];
					if (showAdvertFunc != null)
					{
						showAdvertFunc(int.Parse(jsonObject["ret"].ToString()), text, jsonObject);
					}
					else
					{
						Debuger.Log("展示广告结果回调不存在");
					}
					gdk_game_manager.log("存在");
				}
				else
				{
					Debuger.Log("展示广告结果:广告ID(场景)不存在");
					gdk_game_manager.log("拉取广告失败,广告ID(场景)不存在");
				}
				break;
			}
		}

		public void CreateAdvert(string adunitId, CreateAdvertFunc cb, string adType, string appExtra = "testAppExtra", string extraInfo = "")
		{
			JsonObject jsonObject = new JsonObject();
			jsonObject.Add("appAdTypes", adType);
			jsonObject.Add("placementId", int.Parse(adunitId));
			jsonObject.Add("appExtra", appExtra);
			jsonObject.Add("extraInfo", extraInfo);
			if (cb != null)
			{
				_adVertList[adunitId] = cb;
			}
			string text = SimpleJson.SimpleJson.SerializeObject(jsonObject);
			Debuger.Log("请求创建广告:" + text);
			GameSDK.GamePlat.DoAction(platActionID.action_CreateAdvert, text);
		}

		public void ShowAdvert(string adunitId, JsonObject adInfo, ShowAdvertFunc cb)
		{
			if (adInfo == null)
			{
				adInfo = new JsonObject();
			}
			adInfo.Add("placementId", int.Parse(adunitId));
			if (cb != null)
			{
				_adVertResultList[adunitId] = cb;
			}
			string text = SimpleJson.SimpleJson.SerializeObject(adInfo);
			Debuger.Log("请求展示广告:" + text);
			GameSDK.GamePlat.DoAction(platActionID.action_ShowAdvert, text);
		}

		public void RemoveAdvert(string adunitId, JsonObject adInfo)
		{
			if (adInfo == null)
			{
				adInfo = new JsonObject();
			}
			adInfo.Add("placementId", adunitId);
			string text = SimpleJson.SimpleJson.SerializeObject(adInfo);
			Debuger.Log("移除广告:" + text);
			GameSDK.GamePlat.DoAction(platActionID.action_RemoveAdvert, text);
		}

		public string GetDesignSize()
		{
			return GameSDK.GamePlat.DoAction(platActionID.actionMobileResolution, string.Empty);
		}

		public string GetScale()
		{
			return GameSDK.GamePlat.DoAction(platActionID.atction_GetDesignSize, string.Empty);
		}
	}
}
